home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 3542 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.3 KB

  1. Path: tank.news.pipex.net!pipex!demon!fe-line.demon.co.uk
  2. From: faye@fe-line.demon.co.uk (Faye Pearson)
  3. Newsgroups: comp.lang.c
  4. Subject: Recursive function -> how do you exit one?
  5. Date: Mon, 29 Jan 1996 21:08:15 GMT
  6. Organization: fe-line
  7. Message-ID: <310d27a6.171790522@news.demon.co.uk>
  8. References: <4eh1g8$aba@pulp.ucs.ualberta.ca>
  9. NNTP-Posting-Host: fe-line.demon.co.uk
  10. X-NNTP-Posting-Host: fe-line.demon.co.uk
  11. X-Newsreader: Forte Agent .99d/32.168
  12.  
  13. jbukczyn@gpu.srv.ualberta.ca (Jacob Bukczynski) wrote:
  14.  
  15. >I'm using a recursive function to search for files. I need
  16. >it to quit when a user presses Esc.
  17. >
  18. >Using 
  19. >
  20. >return;
  21. >
  22. >doesn't seem to work, the funtion runs again. ( I tested
  23. >this by putting a printf statement in front of the return -
  24. >it got printed over and over again. )
  25. >
  26. >Is there a special way of exiting a recursive function?
  27.  
  28. You need to return a value which makes all the recursed functions exit
  29. without further processing.
  30.  
  31. if the function returns an int then you could return -1 as aborted.
  32. if it returns a char or char* you can use a (first) character that
  33. cannot exist in your string. eg 0xff
  34.  
  35. char* foo()
  36. {
  37.    result=foo();
  38.    if(*result!=0xff)
  39.    {
  40.       // process things here //
  41.       if (aborted) return "\xff";
  42.    }
  43.    return result;
  44. }
  45.  
  46. Does that help?
  47.  
  48. Faye
  49. --
  50. faye@fe-line.demon.co.uk
  51.